﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NC.Web.Models
{
    public static class HtmlExtensions
    {
        public static string GetQueryJsonStrings(this HttpRequestBase request)
        {
            // 
            if (request.QueryString.Keys.Count == 0)
                return "";
            string resultQuery = "";
            var allKeys = request.QueryString.AllKeys;
            foreach (var allKey in allKeys)
            {
                resultQuery += allKey + "s=" + request.QueryString[allKey] + "&";
            }
            resultQuery = resultQuery.Substring(0, resultQuery.Length - 1);
            return resultQuery;
        }

        public static string GetQueryJsonStringWidthPreFix(this HttpRequestBase request)
        {
            string resultQuery = "?" + GetQueryJsonStrings(request);
            return resultQuery;
        }

        public static MvcHtmlString PageList(this HtmlHelper htmlHelper, string url, int count)
        {
            int pageIndex = HttpContext.Current.Request.QueryString["page"] == null ? 0 : Convert.ToInt32(HttpContext.Current.Request.QueryString["page"].ToString());
            url = AllQueryString(url);
            if (count <= 0)
                return new MvcHtmlString("");
            string html = "<div class='box-footer clearfix'><ul class='pagination pagination-sm no-margin pull-right'><li><a>共有" + count + "条数据</a></li>";
            count = (count - 1) / 10 + 1;
            string firstPage = "<li><a href = '" + url + "page=0' >第一页</a></li>";
            string lastPage = "<li><a href='" + url + "page=" + (count - 1) + "'>最后一页</a></li></ul></div>";

            if (pageIndex > 0)
            {
                if (count > 3)
                {
                    if (pageIndex > 1)
                        html += firstPage;
                    if (pageIndex < count - 2)
                    {
                        html += CompleteLiRow(url, pageIndex);
                        html += lastPage;
                    }
                    else
                    {
                        html += CompleteLiRow(url, count - 3, count, pageIndex);
                    }
                }
                else
                {
                    for (int index = 0; index < count; index++)
                    {
                        if (index == pageIndex)
                            html += LiRow(url, index);
                        else
                            html += LiRow(index, url);
                    }
                }
            }
            else
            {
                if (count < 3)
                {
                    html += LiRow(url, 0);
                    for (int index = 1; index < count; index++)
                    {
                        html += LiRow(index, url);
                    }
                }
                else
                {
                    html += LiRow(url, 0) + LiRow(1, url) + LiRow(2, url);
                    if (pageIndex < count - 1)
                        html += lastPage;
                }
            }
            return new MvcHtmlString(html);
        }

        #region PageListHelper
        private static string CompleteLiRow(string url, int pageIndex)
        {
            return LiRow(pageIndex - 1, url) + LiRow(url, pageIndex) + LiRow(pageIndex + 1, url);
        }
        private static string CompleteLiRow(string url, int start, int end, int pageIndex)
        {
            string html = "";
            for (; start < end; start++)
            {
                if (start == pageIndex)
                    html += LiRow(url, start);
                else
                    html += LiRow(start, url);
            }
            return html;
        }
        private static string LiRow(int pageIndex, string url)
        {
            return "<li><a href='" + url + "page=" + pageIndex + "'>" + (pageIndex + 1) + "</a></li>";
        }
        private static string LiRow(string url, int pageIndex)
        {
            return "<li><a class='bg-blue' href='" + url + "page=" + pageIndex + "'>" + (pageIndex + 1) + "</a></li>";
        }
        private static string AllQueryString(string url)
        {
            string html = url;
            html = html.Contains("?") ? url.Substring(0, url.IndexOf('?') + 1) : (html += "?");
            string[] allKeys = HttpContext.Current.Request.QueryString.AllKeys;
            foreach (var n in allKeys)
            {
                if (n != "page")
                    html += n + "=" + HttpContext.Current.Request.QueryString[n].ToString() + "&";
            }
            return html;
        }
        #endregion
    }
}